package com.mud.dnd.frontend.rs;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.common.QueryParameter.QueryParamOperator;
import com.mud.dnd.domain.model.Action;
import com.mud.dnd.ejb.ActionManager;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.frontend.rs.common.RestInputConverter;



@Stateless
@Path("/Action")
public class ActionJSONService 
{

	
	@EJB
	ActionManager ejbService;
	
	@GET
	@Path("/query")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Action> getActions(@QueryParam("id") Integer id,@QueryParam("name") String name,@QueryParam("desc") String description,@QueryParam("actionID") Integer actionID, @QueryParam("picsURL")String pictureURL,
			  @QueryParam("userName")String userName,@QueryParam("rate") Double rate,@QueryParam("rateOp") String rateOp, @QueryParam("amount")Integer amount, @QueryParam("amountOp")String amountOp) {
		
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try{
		
		
		if(id!=null)
		{
			params.add(new QueryParameter("id", QueryParamOperator.EQUALS, id));
		}
		if(name!=null)
		{
			params.add(new QueryParameter("name", QueryParamOperator.LIKE, name));
		}
		if(description!=null)
		{
			params.add(new QueryParameter("description", QueryParamOperator.LIKE, description));
		}
		if(pictureURL!=null)
		{
			params.add(new QueryParameter("pictureURL", QueryParamOperator.LIKE, pictureURL));
		}
		if(actionID!=null)
		{
			params.add(new QueryParameter("actionID", QueryParamOperator.EQUALS, actionID));
		}
		if(userName!=null)
		{
			params.add(new QueryParameter("author", QueryParamOperator.LIKE, userName));
		}
		if(amount!=null )
		{
			
			params.add(new QueryParameter("amount", RestInputConverter.queryParamOperatorConverter(amountOp), amount));
		}
		if(rate!=null)
		{
			
			params.add(new QueryParameter("rate", RestInputConverter.queryParamOperatorConverter(rateOp), rate));
		}
		}catch(DAOException e)
		{
			return null;
		}
		

 
		return ejbService.getActions(params); 
 
	}
	
	@GET
	@Path("/create")
	@Produces(MediaType.APPLICATION_JSON)
	public String createAction(@QueryParam("name") String name,@QueryParam("desc") String description, @QueryParam("picsURL")String pictureURL,
			 @QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		
		int resultID;
		
		try {
			resultID = ejbService.createAction(name, description, pictureURL, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
 
		return "OK The new action ID:"+String.valueOf(resultID); 
 
	}
	
	@GET
	@Path("/addToCard")
	@Produces(MediaType.APPLICATION_JSON)
	public String addActionToCard(@QueryParam("actionID") int actionID,@QueryParam("cardID") int cardID, @QueryParam("amount")int amount,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID)
	{
		try {
			ejbService.addActiontoCard(actionID, cardID,amount, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		return "OK";
	
	}
	
	@GET
	@Path("/update")
	@Produces(MediaType.APPLICATION_JSON)
	public String updateAction(@QueryParam("id") int id,@QueryParam("name") String name,@QueryParam("desc") String description, @QueryParam("picsURL")String pictureURL,
			  @QueryParam("rate") int rate,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		
		
		try {
			ejbService.updateAction(id,name, description, pictureURL, rate, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}

		return "OK"; 

	}
	
	@GET
	@Path("/delete")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteAction(@QueryParam("id") int id,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		try {
			ejbService.deleteAction(id, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}

		return "OK"; 
	}
	
	@GET
	@Path("/removeFromCard")
	@Produces(MediaType.APPLICATION_JSON)
	public String removeActionFromCard(@QueryParam("actionID") int actionID,@QueryParam("cardID") int cardID,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID)
	{
		try {
			ejbService.removeActionfromCard(actionID, actionID, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		return "OK";
	
	}
	
	
	
}
